<!--短信回执-->
<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="手机号：" name="phone">
						<a-input v-model:value="searchFormState.phone" placeholder="请输入手机号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="供应商：" name="supplierCode">
						<a-select v-model:value="searchFormState.supplierCode">
							<a-select-option
								v-for="option in supplierList"
								:key="option.supplierCode"
								:value="option.supplierCode"
								:label="option.supplierName"
							>
								{{ option.supplierName }}
							</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="发送时间">
						<a-range-picker v-model:value="dateRange" valueFormat="YYYY-MM-DD" :allowClear="false" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:scroll="{ x: 'max-content' }"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #resultCode="{ text, record }">
				<a-tooltip :title="getResultCodeLabel(text, record.supplierCode)">
					<span>{{ text }}</span>
				</a-tooltip>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="Company">
	import smsRecordApi from '@/api/tjzy/sms/smsReceiptApi'
	import supplierApi from '@/api/tjzy/supplier/supplierApi'
	import { yzErrorCode } from '@/utils/smsErrorCode'
	import dayjs from 'dayjs'
	const searchFormRef = ref()
	const searchFormState = ref({
		current: 1,
		size: 10
	})
	const tableRef = ref()
	const supplierList = ref([])
	const toolConfig = { refresh: false, height: false, columnSetting: false, striped: false }
	const columns = [
		{
			title: '供应商',
			dataIndex: 'supplierName'
		},
		{
			title: '短信ID',
			dataIndex: 'msgId'
		},
		{
			title: '回执类型',
			dataIndex: 'flagTypeName'
		},
		{
			title: '手机号',
			dataIndex: 'phone'
		},
		{
			title: '结果代码',
			dataIndex: 'resultCode',
			slots: { customRender: 'resultCode' }
		},
		// {
		// 	title: 'resultName',
		// 	dataIndex: 'resultName'
		// },
		{
			title: '发送时间',
			dataIndex: 'sendTime'
		},
		{
			title: '回传时间',
			dataIndex: 'createTime',
			ellipsis: true
		}
	]
	let dateRange = ref([
		dayjs().subtract(6, 'day').format('YYYY-MM-DD'), // 今天减去7天
		dayjs().format('YYYY-MM-DD') // 今天的日期
	]) //日期范围

	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		searchFormState.value.beginTime = `${dateRange.value[0]} 00:00:00`
		searchFormState.value.endTime = `${dateRange.value[1]} 23:59:59`
		return smsRecordApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}
	const getResultCodeLabel = (code, supplierCode) => {
		let errorCodeList = []
		if (supplierCode === 'YZ') {
			errorCodeList = yzErrorCode
		}
		const errorCode = errorCodeList.find((item) => item.code === code)
		return errorCode ? errorCode.label : '未知错误码'
	}
	//查询表单重置
	const reset = () => {
		searchFormRef.value.resetFields()
		dateRange.value = [
			dayjs().subtract(6, 'day').format('YYYY-MM-DD'), // 今天减去7天
			dayjs().format('YYYY-MM-DD') // 今天的日期
		]
	}

	onMounted(() => {
		supplierApi.list().then((res) => {
			supplierList.value = res
		})
	})
</script>
